package com.smasher.component.activity

import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.widget.Toast
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
import com.smasher.component.helper.NotificationHelper
import com.smasher.widget.base.BaseActivity


abstract class NotificationInit : BaseActivity() {

    val notificationHelper by lazy {
        createNotificationHelper()
    }


    abstract fun createNotificationHelper(): NotificationHelper


    fun getImportance(context: Context, channelId: String) {
        val notificationManager = NotificationManagerCompat.from(context)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = notificationManager.getNotificationChannel(channelId)
            if (channel != null) {
                if (channel.importance == NotificationManager.IMPORTANCE_NONE) {
                    val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
                    intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.id)
                    startActivity(intent)
                    Toast.makeText(this, "升级通知不能关闭，请手动将通知打开", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }
    }


    fun createChannelByConfig(params: MutableList<ChannelParam>): List<NotificationChannelCompat> {
        val list = ArrayList<NotificationChannelCompat>()
        for (channel in params) {
            list.add(
                notificationHelper.createChannel(
                    channel.id,
                    channel.name,
                    channel.description,
                    channel.groupId,
                    channel.showBadge,
                    channel.sound,
                    channel.audioAttributes,
                    channel.lightEnable,
                    channel.vibrationEnabled,
                    channel.importance
                )
            )
        }
        return list
    }

    abstract suspend fun getNotificationConfig(): MutableList<ChannelParam>

    class ChannelParam(
        val id: String,
        val name: String,
        val description: String,
        val groupId: String?,
        val showBadge: Boolean,
        val sound: Uri?,
        val audioAttributes: AudioAttributes?,
        val lightEnable: Boolean,
        val vibrationEnabled: Boolean,
        val importance: Int
    )

    companion object {
        const val TAG = "Notification"

    }

}